package utilities;

import java.util.HashMap;

import implementor.CategoryImp;
import implementor.ChequeDaoImplementor;
import implementor.ColorImp;
import implementor.CustomerImp;
import implementor.PaymentMethodDaoImplementor;
import implementor.SellerInfoImp;
import implementor.SizeImp;
import implementor.TransactionCategoryDaoImplementor;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import dto.Customer;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import daoInterface.CategoryDao;
import daoInterface.ChequeDao;
import daoInterface.ColorDao;
import daoInterface.CustomerDao;
import daoInterface.PaymentMethodDao;
import daoInterface.SellerInfoDao;
import daoInterface.SizeDao;
import daoInterface.TransactionCategoryDao;
import dto.Category;
import dto.Cheque;
import dto.Color;
import dto.PaymentMethod;
import dto.SellerInfo;
import dto.Size;
import dto.TransactionCategory;

public class AppUtilites 
{	
	private static HashMap<String,Integer> Customer;
	private static HashMap<String, Integer> TransCategory;
	private static HashMap<String, Integer> Paymentmethods;
	private static HashMap<String, Integer> Seller;
	private static HashMap<String,Integer> hmSize;
	private static HashMap<String,Integer> hmColor;
	private static HashMap<String,Integer> hmCategory;

	public static void setColorCombo(JComboBox combo)
	{
		ColorDao coldao=new ColorImp();
		Color[] colr = null;
		try {
			colr = coldao.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[colr.length+1];
		items[0]="--Select Color--";
		for(int i=1;i<=colr.length;i++)
		{
			items[i]=colr[i-1].getColourName();
		}
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);		
		
	}
	/**************************************************************/
	public static void setSizeCombo(JComboBox combo)
	{
		SizeDao sd=new SizeImp();
		dto.Size[] s=null;
		try {
			s = sd.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[s.length+1];
		items[0]="--Select Size--";
		for(int i=1;i<=s.length;i++)
		{
			items[i]=s[i-1].getSizeName();
		}
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);		
		
	}
	/**************************************************************/
	public static void setCategoryCombo(JComboBox combo)
	{
		CategoryDao cd=new CategoryImp();
		Category[] c=null;
		try {
			c = cd.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Category--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getLeafName();
		}
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);		
		
	}
	/**************************************************************/
	public static void setSellerNameCombo(JComboBox combo)
	{
		SellerInfoDao sd=new SellerInfoImp();
		SellerInfo[] s=null;
		
		try 
		{
			s = sd.retrieveAll();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		String[] items=new String[s.length+1];
		items[0]="--Select SellerName--";
		
		for(int i=1;i<=s.length;i++)
		{
			items[i]=s[i-1].getSellerName();
		}
		
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);		
	}
	/**************************************************************/
	public static void setAccountingCategoryCombo(JComboBox combo)
	{
		TransactionCategoryDao cd=new TransactionCategoryDaoImplementor();
		TransactionCategory[] c=null;
		try {
			c = cd.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Category--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getcategoryName();
		}
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);		
	}
	/**************************************************************/
	public static void setPaymentMethod(JComboBox combo)
	{
		PaymentMethodDao pd=new PaymentMethodDaoImplementor();
		PaymentMethod[] c=null;
		try {
			c = pd.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Payment Method--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getpaymentMethodName();
		}
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);
	}
	/**************************************************************/
	public static void setBankCombo(JComboBox combo)
	{
		ChequeDao cd=new ChequeDaoImplementor();
		Cheque[] c=null;
		try {
			c = cd.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Bank Name--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getbankName();
		}
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);
	}
	/**************************************************************/
	public static void setBranchNameCombo(JComboBox combo)
	{
		ChequeDao cd=new ChequeDaoImplementor();
		Cheque[] c=null;
		try {
			c = cd.retrieveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Branch Name--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getbranchName();
		}
		ViewUtilites.set_comboBox(combo, items);
		ViewUtilites.setAutoSuggestionsComboBox(combo);
	}
	/**************************************************************/
	public static int getCustomerId(String customerName)
	{
		if(Customer==null)
		{
			Customer = new HashMap<String, Integer>();
			CustomerDao dao=new CustomerImp();
			
			try 
			{
				Customer[]customers=dao.retrieveAll();
				for(Customer temp:customers)
					Customer.put(temp.getCustomerName(),temp.getAutocustomerId());
			}
			
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return Customer.get(customerName);
	}
	/**************************************************************/
	public static int getSellerId(String sellerName)
	{
		if(Seller==null)
		{
			Seller=new HashMap<String, Integer>();
			SellerInfoDao sid=new SellerInfoImp();
			
			try 
			{
				SellerInfo[] sellers=sid.retrieveAll();
				for(SellerInfo temp:sellers)
					Seller.put(temp.getSellerName(), temp.getautoSellerId());
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		System.out.println("The name of seller is "+sellerName);
		return Seller.get(sellerName);
	}
	/**************************************************************/
	public static int getSizeId(String sizename)
	{
		if(hmSize==null)
		{
			hmSize=new HashMap<String, Integer>();
			SizeDao sd=new SizeImp();
			
			try 
			{
				Size[] sizes=sd.retrieveAll();
				for(Size temp:sizes)
					hmSize.put(temp.getSizeName(),temp.getautosizeCode());
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		return hmSize.get(sizename);
	}
	/**************************************************************/
	public static int getColorId(String colorname)
	{
		if(hmColor==null)
		{
			hmColor=new HashMap<String, Integer>();
			ColorDao cd=new ColorImp();
			
			try 
			{
				Color[] colors=cd.retrieveAll();
				for(Color temp:colors)
					hmSize.put(temp.getColourName(),temp.getcolorCode());
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		return hmSize.get(colorname);
	}
	/**************************************************************/
	public static int getCategoryId(String categoryname)
	{
		if(hmCategory==null)
		{
			hmCategory=new HashMap<String, Integer>();
			CategoryDao cd=new CategoryImp();
			
			try 
			{
				Category[] categories=cd.retrieveAll();
				for(Category temp:categories)
					hmCategory.put(temp.getLeafName(),temp.getAutoId());
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		return hmCategory.get(categoryname);
	}
	/**************************************************************/
	public static int getTransCatId(String categoryName)
	{
		if(TransCategory==null)
		{
			TransCategory = new HashMap<String, Integer>();
			TransactionCategoryDao dao=new TransactionCategoryDaoImplementor();
			try {
				TransactionCategory[]category=dao.retrieveAll();
				for(TransactionCategory temp:category)
				   TransCategory.put(temp.getcategoryName(),temp.getautoTransCatId());
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return TransCategory.get(categoryName);
	}
	/**************************************************************/
	public static int getPaymentMethodId(String payMethodName)
	{
		if(Paymentmethods==null)
		{
			Paymentmethods = new HashMap<String, Integer>();
			PaymentMethodDao dao=new PaymentMethodDaoImplementor();
			try {
				PaymentMethod[] paymethod=dao.retrieveAll();
				for(PaymentMethod temp:paymethod)
				   Paymentmethods.put(temp.getpaymentMethodName(),temp.getautoPaymentMethodId());
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return Paymentmethods.get(payMethodName);
	}
	/**************************************************************/
	public static void getTime(JTextField tf)
	{
		DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		   //get current date time with Date()
		   Date date = new Date();
		   System.out.println(dateFormat.format(date));
	
		   //get current date time with Calendar()
		   Calendar cal = Calendar.getInstance();
		   System.out.println(dateFormat.format(cal.getTime()));
		   tf.setText(dateFormat.format(cal.getTime()));
	}
	/**************************************************************/
}
